package kr.hkit.powerjava.chapter17;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TextAreaTest extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panel;
	private JTextField textField;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TextAreaTest frame = new TextAreaTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TextAreaTest() {
		initialize();
	}
	private void initialize() {
		setTitle("Text Area Test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panel = new JPanel();
		contentPane.add(panel, BorderLayout.NORTH);
		
		textField = new JTextField(30);
		textField.addActionListener(this);
		panel.add(textField);
		textField.setColumns(10);
		
		textArea = new JTextArea(10,30);
		contentPane.add(textArea, BorderLayout.CENTER);
		
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == textField) {
			actionPerformedTextField(e);
		}
	}
	protected void actionPerformedTextField(ActionEvent e) {
		String text=textField.getText();
		textArea.append(text + "\n");
		textField.selectAll();
		textArea.setCaretPosition(textArea.getDocument().getLength());
	}
}
